package com.asset.dispoal.agent.service.common;

import com.asset.dispoal.agent.domain.db.entity.SysAgent;
import com.asset.dispoal.agent.domain.db.mapper.SysAgentMapper;
import com.asset.dispoal.agent.dto.SysAgentRequest;
import com.asset.dispoal.agent.dto.SysAgentResponse;
import com.asset.dispoal.agent.service.converter.SysAgentConverter;
import com.asset.dispoal.commom.page.PagingQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @author qr
 * @date 2022/3/25$
 */
@Component
public class SysAgentQueryPageCommand implements BaseCommand<SysAgentRequest, Result<PageVO<SysAgentResponse>>> {


    @Autowired
    SysAgentMapper sysAgentMapper;

    @Override
    public Result<PageVO<SysAgentResponse>> execute(SysAgentRequest request) {
        SysAgent sysAgent = SysAgentConverter.INSTANCE.resToDo(request);
        int count = sysAgentMapper.count(sysAgent);
        Page<SysAgent> page = PagingQuery.getPage(count, sysAgent);
        PageVO<SysAgentResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);
        if (page != null) {
            List<SysAgent> mapperList = sysAgentMapper.page(page);
            List<SysAgentResponse> sysAgentResponses = SysAgentConverter.INSTANCE.dosToRes(mapperList);
            pageVO.setResult(sysAgentResponses);
        }
        return Result.ok(pageVO);
    }
}
